//
//  NOConnectDeviceVC.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/11.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "NOConnectDeviceVC.h"
#import "NOConnectDeviceModel.h"

#define kFirstCellIdentifier @"FirstCellIdentifier"
#define kSecondCellIdentifier @"kSecondCellIdentifier"
#define kThreeCellIdentifier    @"kThreeCellIdentifier"

@interface NOConnectDeviceVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UIButton *addSectionBtn;

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSArray *dataArr;



@end

@implementation NOConnectDeviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.addSectionBtn];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dict = self.dataArr[section];
    if (section == 0) {
        
        // 以分组
        int count = 0;
        NSArray *arr = dict[@"value"];
        for(int i = 0; i < arr.count ;i ++ ){
            
            NOConnectDeviceModel *model = arr[i];
            count++;
            
            for (NSInteger j = 0;  j < model.models.count ; j++) {
                
                // 计算总有多少行
                count++;
            }
        }
        
        return count;
        
    } else {
        // 未分组
        return [dict[@"value"] count];
    }
    
    
    return [dict[@"value"] length];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [UITableViewCell new];
    cell.backgroundColor = [UIColor orangeColor];
    return cell;
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - set & get
- (UIButton *)addSectionBtn {
    
    if (!_addSectionBtn) {
        
        _addSectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addSectionBtn setTitle:NSLocalizedString(@"ME_NOConnection_DeviceList", nil) forState:UIControlStateNormal];
        [_addSectionBtn setTitleColor:SEPARATOR_BG_COLOR forState:UIControlStateNormal];
        _addSectionBtn.frame = CGRectMake(0, 0, 80, 30);
        _addSectionBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        
    }
    return _addSectionBtn;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"ZMEDeviceSecondCell" bundle:nil] forCellReuseIdentifier:kSecondCellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"ZMEDeviceThreeCell" bundle:nil] forCellReuseIdentifier:kThreeCellIdentifier];
    }
    
    return _tableView;
}


#pragma mark - set & get
- (NSArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = @[@{@"name":@"设备列表",
                       @"value":[NSMutableArray array]
                       },
                     @{@"name":@"未分组列表",
                       @"value":[NSMutableArray array]
                       }];
        
        
        // 给设备列表添加假数据
        NOConnectDeviceModel *listModel0 = [NOConnectDeviceModel new];
        listModel0.secondIndex = YES;
        listModel0.title = @"晚霞霓虹灯常跑亮-1";
        NOConnectDeviceModel *listModel0_1 = [NOConnectDeviceModel new];
        listModel0_1.threeIndex = YES;
        listModel0_1.title = @"震动-1";
        NOConnectDeviceModel *listModel0_2 = [NOConnectDeviceModel new];
        listModel0_2.threeIndex = YES;
        listModel0_2.title = @"摇头-2";
        [listModel0.models addObject:listModel0_1];
        [listModel0.models addObject:listModel0_2];
        
        NOConnectDeviceModel *listModel1 = [NOConnectDeviceModel new];
        listModel1.secondIndex = YES;
        listModel1.title = @"晚霞霓虹灯常跑亮-2";
        NOConnectDeviceModel *listModel1_1 = [NOConnectDeviceModel new];
        listModel1_1.threeIndex = YES;
        listModel1_1.title = @"震动-1";
        [listModel1.models addObject:listModel1_1];
        
        [_dataArr[0][@"value"] addObject:listModel0];
        [_dataArr[0][@"value"] addObject:listModel1];
        
  
        
        // 给未分组添加假数据
        NOConnectDeviceModel *noList1 = [NOConnectDeviceModel new];
        noList1.threeIndex = YES;
        noList1.title = @"摇头-1";
        NOConnectDeviceModel *noList2 = [NOConnectDeviceModel new];
        noList2.threeIndex = YES;
        noList2.title = @"摇头-2";
        [_dataArr[1][@"value"]  addObject:noList1];
        [_dataArr[1][@"value"] addObject:noList2];
    }
    return _dataArr;
}


@end
